//
//  TabViewController.m
//  jinnengExam
//
//  Created by mac on 2020/8/8.
//  Copyright © 2020 jznrj. All rights reserved.
//

#import "TabViewController.h"
#import "LearnViewController.h"
#import "VideoViewControllerViewController.h"
#import "UserCenterViewController.h"
#import "LoginViewController.h"
#import "ExamViewController.h"
#import "BookViewController.h"
#import "ArtistViewController.h"
@interface TabViewController ()<UITabBarDelegate>

@end

@implementation TabViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    
    UINavigationController *vc0 = [[UINavigationController alloc]initWithRootViewController:ExamViewController.new];
    vc0.tabBarItem.title = @"学习考试";
    vc0.tabBarItem.image = [UIImage imageNamed:@"jzn_exam.png"];
    vc0.tabBarItem.selectedImage = [UIImage imageNamed:@"exam_select.png"];
    vc0.tabBarItem.tag = 0;
    
    UINavigationController *vc1 = [[UINavigationController alloc]initWithRootViewController:VideoViewControllerViewController.new];
    vc1.tabBarItem.title = @"在线视听";
    vc1.tabBarItem.image = [UIImage imageNamed:@"jzn_exam.png"];
    vc1.tabBarItem.selectedImage = [UIImage imageNamed:@"exam_select.png"];
    vc1.tabBarItem.tag = 1;
  
    UINavigationController *vc2 = [[UINavigationController alloc]initWithRootViewController:LearnViewController.new];
    vc2.tabBarItem.title = @"学习";
    vc2.tabBarItem.image = [UIImage imageNamed:@"newbie.png"];
    vc2.tabBarItem.selectedImage = [UIImage imageNamed:@"newbie_select.png"];
    vc2.tabBarItem.tag = 2;
    
    UINavigationController *vc3 = [[UINavigationController alloc]initWithRootViewController:BookViewController.new];
    vc3.tabBarItem.title = @"晋能书院";
    vc3.tabBarItem.image = [UIImage imageNamed:@"skill.png"];
    vc3.tabBarItem.selectedImage = [UIImage imageNamed:@"skill_select.png"];
    vc3.tabBarItem.tag = 3;
    
    UINavigationController *vc4 = [[UINavigationController alloc]initWithRootViewController:UserCenterViewController.new];
    vc4.tabBarItem.title = @"个人中心";
    vc4.tabBarItem.image = [UIImage imageNamed:@"user.png"];
    vc4.tabBarItem.selectedImage = [UIImage imageNamed:@"user_select.png"];
    vc4.tabBarItem.tag = 4;
    
//    UINavigationController *vc4 = [[UINavigationController alloc]initWithRootViewController:UserCenterViewController.new];
//    vc4.tabBarItem.title = @"个人中心";
//    vc4.tabBarItem.image = [UIImage imageNamed:@"user.png"];
//    vc4.tabBarItem.selectedImage = [UIImage imageNamed:@"user_select.png"];
//    vc4.tabBarItem.tag = 3;
      
    self.tabBar.tintColor = [ColorUtil titleColor];
    self.tabBar.backgroundColor = [UIColor whiteColor];
    self.tabBar.translucent = YES;

    self.viewControllers = @[vc0,vc1,vc2,vc3,vc4];
}
-(void) viewDidAppear:(BOOL)animated{
    [self relogin];
   
}
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    if(item.tag != 1 && item.tag != 2) {
        NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
        if ([userDefault objectForKey:@"account"] == nil) {
            LoginViewController *controller = [[LoginViewController alloc]init];
            controller.tabIndex = item.tag;
            controller.modalPresentationStyle = UIModalPresentationFullScreen;
            [self presentModalViewController:controller animated:YES ];
            return;
        }
    }

}


-(void) relogin{
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    if ([userDefault objectForKey:@"account"] != nil) {
        NSString *url = [NSString stringWithFormat:@"%@/user/userLogin" ,[URLUtil hostName]];
        NSDictionary *params = @{@"userMobile":[userDefault objectForKey: @"account"],
                                 @"password":[userDefault objectForKey: @"pwd"]
                                 };
        [JZNRequestUtil requestUrl:url requestParams:params success:^(DataResult *dataResult) {
            if(dataResult.code == 0){
                
                NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
                 [userDefault setObject:dataResult.token forKey:@"token"];
                 [userDefault setObject:[dataResult.data mj_JSONString] forKey:@"userInfo"];
                 [self repostResult];
                
            }else {
                [userDefault setObject:nil forKey:@"account"];
            }
        } failure:^(NSError *error) {
            
        }];
        
    }
}

-(void) repostResult {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSDictionary *result =[userDefaults objectForKey:@"online_exam_result"];
    if(result!= nil){
        NSString *url = [NSString stringWithFormat:@"%@/onlineExam/submitOnlineExamPaper",[URLUtil hostName]];
        [JZNRequestUtil requestUrl:url requestParams:result success:^(DataResult *dataResult) {
            if(dataResult.code == 0){
                
                NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
                [userDefaults removeObjectForKey:@"online_exam_result"];
                [[UIApplication sharedApplication].delegate.window makeToast:@"提交成绩成功"];
            }else {
            }
        } failure:^(NSError *error) {
            
        }];
    }
   
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
